function [] = SETAShddBGC_SurfMap(varname,ncfilename)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% THIS IS THE MAIN ROUTINE TO PRODUCE AN ANIMATED SURFACE MAP OF 
% A SETAS BIOGEOCHEMICAL MODEL MONTHLY MEAN VARIABLE 
%  
% INPUT: - varname      : a SETAS variable. The available option 'Chl_a'
%        - ncfilename   : a netcdf SETAS BGC hdd model outpout file. Note
%                         that this file has been written for a file with
%                         a specific simulated time period.
%                     
% Author: Benedicte Pasquer, IMOS/eMII (http://imos.org.au/)
% email: benedicte.pasquer@utas.edu.au
% May 2013   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


ncid = netcdf.open(ncfilename,'NC_NOWRITE');    
varid = netcdf.inqVarID(ncid,'x_centre') ;%lon at centre of cell
lon = netcdf.getVar(ncid,varid) ;
varid = netcdf.inqVarID(ncid,'y_centre') ;%lat at centre of cell
lat = netcdf.getVar(ncid,varid) ;
varid = netcdf.inqVarID(ncid,'z_centre') ;%lat at centre of cell
zc = netcdf.getVar(ncid,varid) ;
varid = netcdf.inqVarID(ncid,'t') ;
time =netcdf.getVar(ncid,varid) ;
varid = netcdf.inqVarID(ncid,'Chl_a') ;
Chla = netcdf.getVar(ncid,varid,'double') ;
netcdf.close(ncid);

% EXTRACT 4 LAYERS CORRESPONDING TO TOP 2METERS AND FIRST 31 TIME STEPS 
% CORRESPONDING TO MONTH OF MARCH
k = 31 ; %DAILY TIME STEP, FIRST 31 DAYS CORRESPOND TO MONTH OF MARCH
var = Chla(:,:,22:25,1:k); 
% COMPUTE THE SURFACE MEAN 
var = mean(var,3);
var = squeeze(var);

% CREATE AN ANIMATED SURFACE MAP OF A  HIGH RESOLUTION 
% SETAS BGC VARIABLE

animated(1,1,1,k) =0;

for n = 1:k
    
       [frame] = BGC_plot_map(lon,lat,var(:,:,n));

 if n == 1
      [animated, cmap] = rgb2ind(frame.cdata, 256, 'nodither');
   else
      animated(:,:,1,n) = rgb2ind(frame.cdata, cmap, 'nodither');
 end
close(gcf)
end
% OUTPUT FILE NAME 
filename = strcat(varname,'March2009.gif');
% GENERATION OF THE ANIMATED GIF
imwrite(animated,cmap,filename,'DelayTime',.25,'Loopcount',Inf)


